home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / std / c / 151 < prev    next >
Text File  |  1996-08-06  |  3KB  |  75 lines

  1. Path: boy.nmd.msu.ru!not-for-mail
  2. From: krotoff@such.srcc.msu.su (Alexander Krotoff)
  3. Newsgroups: comp.std.c
  4. Subject: ... char * * promotion to char const * const * ...
  5. Date: 19 Jan 1996 13:33:44 +0300
  6. Organization: Research Computer Center, Moscow State University
  7. Sender: krotoff@boy.nmd.msu.ru
  8. Message-ID: <4dns28$3c5@boy.nmd.msu.ru>
  9. References: <4dgj8q$qin@unix.sri.com> <KANZE.96Jan17121659@slsvewt.lts.sel.alcatel.de> <DLBzGB.J60@polo.demon.co.uk>
  10. Reply-To: krotoff@such.srcc.msu.su (Alexander Krotoff)
  11. NNTP-Posting-Host: boy.nmd.msu.ru
  12.  
  13. X-InCommentTo: john@polo.demon.co.uk (John Winters)
  14.  
  15. john@polo.demon.co.uk (John Winters) wrote:
  16. > In article <KANZE.96Jan17121659@slsvewt.lts.sel.alcatel.de>,
  17. > James Kanze US/ESC 60/3/141 #40763 <kanze@lts.sel.alcatel.de> wrote:
  18. > [snip]
  19. > >
  20. > >Originally, the C standard was going to allow this; in fact, it was
  21. > >going to allow all casts which added const anywhere in the type.  Then
  22. > >someone pointed out that the conversion 'char ** -> char *const *' was
  23. > >unsafe.  As a result, the wording was changed to only allow adding the
  24. > >const at the top level.
  25. > Interesting.  Could you enlighten us (well, me anyway) as to *why* it
  26. > is unsafe.  I don't find it immediately obvious.
  27.  
  28. Hello Joshn.
  29.  
  30. here is an example Joe Buck sent me this Julay.
  31. i did not understand it too before.
  32. i think it is quite simple and clean.
  33.  
  34. Regards,
  35. --
  36. Alexander N. Krotoff
  37. Research Computer Center
  38. Moscow State University
  39.  
  40. -------------------------------------------------------------------
  41. From: Joe Buck <jbuck@Synopsys.COM>
  42. Message-Id: <199507211636.JAA17469@deerslayer.synopsys.com>
  43. Subject: Re: pointer conversions (minor)
  44. To: krotoff@such.srcc.msu.su (Alexander Krotoff)
  45. Date: Fri, 21 Jul 95 9:36:39 PDT
  46. In-Reply-To: <9507211206.AA00906@such.srcc.msu.su>; from "Alexander Krotoff" at Jul 21, 95 3:10 pm
  47. X-Mailer: ELM [version 2.3 PL11]
  48.  
  49. > As seems to me it's failed just becouse `ch_p' and `ch_pp' are never
  50. > initialized ;-) Is your example complete ?
  51.  
  52. I apologize for the error.  Here is a correct example.  I saw the
  53. segmentation fault and assumed it was due to accessing the const,
  54. but the const object is on the stack so it's not in read-only memory.
  55.  
  56. #include <iostream.h>
  57.  
  58. int main() {
  59.     const char ** const_ch_pp;
  60.     char * ch_p;
  61.     char ** ch_pp = & ch_p;
  62.     const char const_ch = 'a';
  63.     const char * const_ch_p = &const_ch;
  64.  
  65.     cout << "const_ch_p points to " << *const_ch_p << endl;
  66.     const_ch_pp = ch_pp;        // illegal, but you want it legal
  67.     *const_ch_pp = const_ch_p;  // copy const ptr to non-const ptr
  68.     *ch_p = 'b';                // now we can change the const object
  69.     cout << "const_ch_p points to " << *const_ch_p << endl;
  70. }
  71.  
  72. It's simply not debatable: it is unsafe to assign a char** to a const char**.
  73. --------------------------------------------------------------------
  74.